Skip to content

PEAR/ObjectOperatorIndent: improve end of statement detection when getting the next object operator #1158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

rodrigoprimo
Copy link
Contributor

@rodrigoprimo rodrigoprimo commented Jul 1, 2025

Description

This PR fixes a bug where PEAR.WhiteSpace.ObjectOperatorIndent would generate a false positive when checking multiple chained object operators in a multidimensional array.

The problem is that the code was using the $local parameter of findNext() to limit the scope of the search when reassigning $next to the next object operator, and $local only considers a semicolon as the end of a statement.

Different chained calls are not necessarily separated by a semicolon (as shown in the test added in this commit). This means that the sniff currently considers the object operators on the second and third chained method calls as part of the first chained method call and checks their indentation using the indentation of the first element on the first chained call as its base.

To fix this problem, instead of relying on the $local parameter to find the end of the statement, the end of the statement as defined by File::findEndOfStatement() is now used.

See #1154 for more details on the problem.

Suggested changelog entry

Fixed: false positive when PEAR.WhiteSpaceObjectOperatorIndent checks multiple chained method calls in a multidimensional array

Related issues/external references

Fixes #1154
Fixes squizlabs/PHP_CodeSniffer#3718

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

…tting the next object operator

This commit fixes a bug where `PEAR.WhiteSpace.ObjectOperatorIndent`
would generate a false positive when checking multiple chained object
operators in a multidimensional array.

The problem is that the code was using the `$local` parameter of
`findNext()` to limit the scope of the search when reassigning
`$next` to the next object operator, and `$local` only considers a
semicolon as the end of a statement.

Different chained calls are not necessarily separated by a semicolon (as
shown in the test added in this commit). This means that the sniff
currently considers the object operators on the second and third chained
method calls as part of the first chained method call. The sniff checks
their indentation using the indentation of the first element on the
first chained call as its base.

To fix this problem, instead of relying on the `$local` parameter to
find the end of the statement, the end of the statement as defined by
`File::findEndOfStatement()` is now used.
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigoprimo Good analysis and the fix makes sense to me. Thank you for taking this one on!

@jrfnl jrfnl added this to the 3.13.3 milestone Jul 2, 2025
@jrfnl jrfnl merged commit 79c5a18 into PHPCSStandards:master Jul 2, 2025
50 checks passed
@jrfnl jrfnl deleted the fix-object-operator-indent branch July 2, 2025 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants